(function () {
    // 验证码
    const imgcode = document.querySelector('.imgcode');
    imgcode.addEventListener('click', imgCodeFn);
    function imgCodeFn() {
        this.src = '/api/code?' + new Date().valueOf();
    }
    //注册
    let btnLogin = document.querySelector('.btn-login.reg');
    btnLogin.addEventListener('click', regFn);

    function regFn() {
        /** 获取输入框输入的信息 */
        const tel = document.querySelector('input[name="regtel"]').value;
        const passwd = document.querySelector('input[name="regpasswd"]').value;
        const code = document.querySelector('input[name="code"]').value;
        const tip3 = document.querySelector('.tip3');
        tip3.style.display = 'block';
        $(function () {
            $('#tip3').addClass('animated bounceInDown');
        });
        setTimeout(() => {
            tip3.style.display = 'none';
        }, 3000)

        /** 把数据提交给后端进行验证 */
        // console.log(tel, passwd, code);
        axios.post('/api/reg', {
            tel, passwd, code
        }).then(function (res) {
            const r = res.data;
            console.log(r);
            if (r.code === 1) {
                tip3.innerHTML = r.msg;
                setTimeout(() => {
                    window.location.href = './index.html';
                }, 1000)
            } else if (r.code === -1) {
                tip3.innerHTML = r.msg;
            } else if (r.code === 0) {
                tip3.innerHTML = r.msg;
            } else {
                tip3.innerHTML = r.msg;
            }
        }).catch(function (error) {
            console.log(error);
        });
    }
}
)();
